#include "st7567a.h"

#include "image.h"

void lag(void) {
    for(uint32_t i = 0; i < 1000; i++){
        i = i;
    }
}


unsigned char i;
void write_com(unsigned char byte)
{
	LCD_A0_GPIO_Port->BRR = (uint32_t) LCD_A0_Pin;
	LCD_SCK_GPIO_Port->BSRR = (uint32_t) LCD_SCK_Pin;
	LCD_CS_GPIO_Port->BRR = (uint32_t) LCD_CS_Pin;
	
	for(i=0;i<8;i++)
	{
		LCD_SCK_GPIO_Port->BRR = (uint32_t) LCD_SCK_Pin;
		(byte&0x80) ? (LCD_SDI_GPIO_Port->BSRR = (uint32_t) LCD_SDI_Pin) : (LCD_SDI_GPIO_Port->BRR = (uint32_t) LCD_SDI_Pin);
		byte <<= 1;
		lag();
		LCD_SCK_GPIO_Port->BSRR = (uint32_t) LCD_SCK_Pin;
		lag();
	}
	LCD_CS_GPIO_Port->BSRR = (uint32_t) LCD_CS_Pin;
}

void write_dat(unsigned char byte)
{
	LCD_A0_GPIO_Port->BSRR = (uint32_t) LCD_A0_Pin;
	LCD_SCK_GPIO_Port->BSRR = (uint32_t) LCD_SCK_Pin;
	LCD_CS_GPIO_Port->BRR = (uint32_t) LCD_CS_Pin;
	
	for(i=0;i<8;i++)
	{
		LCD_SCK_GPIO_Port->BRR = (uint32_t) LCD_SCK_Pin;
		(byte&0x80) ? (LCD_SDI_GPIO_Port->BSRR = (uint32_t) LCD_SDI_Pin) : (LCD_SDI_GPIO_Port->BRR = (uint32_t) LCD_SDI_Pin);
		byte <<= 1;
		lag();
		LCD_SCK_GPIO_Port->BSRR = (uint32_t) LCD_SCK_Pin;
		lag();
	}
	LCD_CS_GPIO_Port->BSRR = (uint32_t) LCD_CS_Pin;
}

#define uchar unsigned char
#define uint unsigned int

void icon(uchar da)
{
	uchar j;
	write_com(0xb8);			
	write_com(0x10);	
	write_com(0x00);
	for(j=0;j<132;j++)
	{
	   write_dat(da);
	}
}



void line(uchar da)
{
	uchar i,j;
	for(i=0xb0;i<0xb8;i++)
	{
	  write_com(i);			
	  write_com(0x10);	
	  write_com(0x00);
	  for(j=0;j<96;j++)
	  {
	     write_dat(da);
	  }
	}
}

void ver(unsigned char da,unsigned char db,unsigned char dc,unsigned char dd)
{
	unsigned char i,j;
	for(j=0;j<9;j++)	
	{
		write_com(0xb0+j);	//64ѐΪ}
		write_com(0x10);	
	  write_com(0x00);
		for(i=0;i<24;i++)		//96Ϊ}ì96/4=24
		{
	 		write_dat(da);
			write_dat(db);
			write_dat(dc);
			write_dat(dd);
		}
	}
}



//ͼ����ʾ
void lcd_image(unsigned char *p)
{
    unsigned char i,j;	
	for (i=0xb0;i<0xb8;i++)
	{
	  write_com(i);			// RAM page address
	  write_com(0x10);		// RAM column address most address
	  write_com(0x00);		// RAM column address least address
	  for(j=0;j<96;j++)
	  {
	     write_dat(*p++);
	  }
	}
   
}



uint8_t contrast = 50;
uint8_t isDelay = 0;

#define BIAS_1P9 0X0
#define BIAS_1P8 0X4
#define BIAS_1P7 0X1
#define BIAS_1P6 0X5
#define BIAS_1P5 0X3
#define BIAS_1P4 0X6

uint8_t isBias1P7 = 1;


void st7567a_init_old(void) {
	contrast = 54;

	write_com(0xe2);	//ɭݾشλ
	write_com(0xa3);	//1/7bias
	write_com(0xa0);	//seg direction
	write_com(0xc8);	//com direction
	write_com(0x24);	//resistor ratio(RR)
	write_com(0x81);	
	write_com(contrast);		//set V0 output voltage,mÛֵ݆̣Ϊ9.41(ʵӢ9.37-9.39V)
	write_com(0x2f);	//power control set
	write_com(0x40);	//display start line
	write_com(0xb0);	//page address
	write_com(0x10);	//column address(msb)
	write_com(0x00);	//column address(lsb)
	write_com(0xa4);	//normal display
	write_com(0xaf);	//display on

}

void st7567a_init_new(void) {
	contrast = 50;
	
	write_com(0xe2);	//ɭݾشλ
	write_com(0xa2);	//1/7bias a3
	write_com(0xa0);	//seg direction
	write_com(0xc8);	//com direction
	write_com(0x25);	//resistor ratio(RR)24
	write_com(0x81);	
	write_com(contrast);		//set V0 output voltage,mÛֵ݆̣Ϊ9.41(ʵӢ9.37-9.39V)
	write_com(0x2f);	//power control set
	write_com(0x40);	//display start line
	write_com(0xb0);	//page address
	write_com(0x10);	//column address(msb)
	write_com(0x00);	//column address(lsb)
	write_com(0xa4);	//normal display
	
	write_com(0xf8);
	write_com(0x01);	
	
	write_com(0xaf);	//display on
	
	write_com(0xff);
	write_com(0x64);	// 180521_105740   6b
	write_com(0xfe);
	
	write_com(0xff);
    write_com(0x72);	
	write_com(0xfe);
	write_com(0xd4);
    write_com(0x93);
	write_com(0x98);

}
uint8_t pause = 0;
void delay_pause(uint32_t ms) {

	while(ms--){

		HAL_Delay(1);
		while(pause);
	}
}

void st7567a_test(void)
{
    HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_RESET);
	HAL_Delay(100);
	//res=1;
    HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_SET);

	//cs=0;
    HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET);

	st7567a_init_old();
 
	while(1)
	{
		line(0xff);			//ȫД
		icon(0x01);			//iconѐȫД
		isDelay = 1;
		delay_pause(1000);
		isDelay = 0;

		line(0x11);icon(0x01);	
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;

		line(0x22);icon(0x00);	
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;

		line(0x44);icon(0x00);	
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;

		line(0x88);icon(0x00);	
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;

		ver(0xff,0x00,0x00,0x00);
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;

		ver(0x00,0xff,0x00,0x00);
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;
		
		ver(0x00,0x00,0x00,0xff);
		isDelay = 1;
		delay_pause(200);		
		isDelay = 0;

		ver(0x00,0x00,0xff,0x00);
		isDelay = 1;
		delay_pause(200);
		isDelay = 0;

		lcd_image(image_yes1);//ͼƬ
		isDelay = 1;
		delay_pause(1000);
		isDelay = 0;

        HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET);
		lcd_image(image_cs);
        isDelay = 1;
		isDelay = 0;

		HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET);
	    lcd_image(image_chess);
  	    isDelay = 1;
		delay_pause(1000);
		isDelay = 0;


	}
}

void HAL_GPIO_EXTI_Callback(uint16_t pin) {

	if(isDelay) {
		if(pin == KEY1_Pin) {
			if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET) {
				
				/*
				//切换bias
				write_com(0xff);
				write_com(0x72);	
				write_com(0xfe);
				write_com(0xd4);
				if(isBias1P7) {
					write_com(0x90+BIAS_1P5);
					isBias1P7 = 0;
				} else {
					write_com(0x90+BIAS_1P7);
					isBias1P7 = 1;
				}
				write_com(0x98);
				*/
				
				if(pause == 0) {

					pause = 1;
				} else {
					pause = 0;
				}
			}
		}

		if(pin == KEY2_Pin) {
			if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET) {
				contrast--;
				write_com(0x81);
				write_com(contrast);
			}
		}

		if(pin == KEY3_Pin) {
			if(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_RESET) {
				contrast++;
				write_com(0x81);
				write_com(contrast);
			}
		}

	}	
}